package de.lulusoft.anothertorrent.gui.commands;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.PlatformUI;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;

public class DeleteTorrentCommand implements IHandler {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.commands.DeleteTorrentCommand";

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		if (event.getTrigger() instanceof Torrent)
		{
			Torrent t = (Torrent)event.getTrigger();
			MessageBox confirm = new MessageBox(
					PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
					SWT.ICON_QUESTION | SWT.YES | SWT.NO);
			confirm.setMessage("Do you really want to delete the torrent '" 
					+ t.getName() + "'?");
			confirm.setText("Delete Torrent?");
			if (confirm.open() == SWT.YES)
			{
				try {
					t.getServer().getRTorrentManager().torrentErase(t.getHash());
					t.setState(1L);
				} catch (Exception e) {
					e.printStackTrace();
					throw new ExecutionException("Could not delete torrent", e);
				}		
			}
				
		}		
		return null;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public boolean isHandled() {
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
	}

}
